// 配网状态枚举
import 'package:tuya_home_sdk_flutter/tuya_home_sdk_flutter.dart';

enum PairState { idle, process, failed, success }

// 设备配网状态记录
class DevicePairState {
  final ThingSmartDeviceModel? deviceModel;
  final PairState state;
  final int errorCode;
  final String errorMsg;
  final bool inQueue;

  factory DevicePairState.initial() {
    return DevicePairState(
      deviceModel: null,
      state: PairState.idle,
      errorCode: 0,
      errorMsg: '',
      inQueue: false,
    );
  }

  DevicePairState({
    required this.deviceModel,
    required this.state,
    required this.errorCode,
    required this.errorMsg,
    this.inQueue = false,
  });

  DevicePairState copyWith({
    ThingSmartDeviceModel? deviceModel,
    PairState? state,
    int? errorCode,
    String? errorMsg,
    bool? inQueue,
  }) {
    return DevicePairState(
      deviceModel: deviceModel ?? this.deviceModel,
      state: state ?? this.state,
      errorCode: errorCode ?? this.errorCode,
      errorMsg: errorMsg ?? this.errorMsg,
      inQueue: inQueue ?? this.inQueue,
    );
  }

  String get outErrorMsg {
    switch (errorCode) {
      case 1:
        return "设备接收的数据包格式错误";
      case 2:
        return "设备找不到路由器";
      case 3:
        return "Wi-Fi 密码错误";
      case 4:
        return "设备连不上路由器";
      case 5:
        return "设备 DHCP 失败";
      case 6:
        return "设备连云失败";
      case 100:
        return "用户取消配网";
      case 101:
        return "蓝牙连接错误";
      case 102:
        return "发现蓝牙服务错误";
      case 103:
        return "打开蓝牙通讯通道失败";
      case 104:
        return "蓝牙查询设备信息失败";
      case 105:
        return "蓝牙配对失败";
      case 106:
        return "配网超时";
      case 107:
        return "Wi-Fi 信息发送失败";
      case 108:
        return "Token 失效";
      case 109:
        return "查询蓝牙加密密钥失败";
      case 110:
        return "设备不存在";
      case 111:
        return "设备云端注册失败";
      case 112:
        return "设备云端激活失败";
      case 113:
        return "云端设备已被绑定";
      case 114:
        return "主动断开";
      case 115:
        return "云端查询设备信息失败";
      case 116:
        return "设备此时正被其他方式配网";
      case 117:
        return "OTA 升级失败";
      case 118:
        return "OTA 升级超时";
      case 119:
        return "Wi-Fi 配网传参校验失败";
      case 207001:
        return "蓝牙服务不支持";
      case 207002:
        return "无效的 HomeId";
      case 207003:
        return "缺失入参";
      case 207004:
        return "无效的 Timeout";
      case 207005:
        return "设备类型不支持";
      case 207006:
        return "正在配网";
      case 207007:
        return "配网超时";
      case 207008:
        return "Token 过期";
      case 207009:
        return "未知错误";
      case 207010:
        return "设备服务不支持";
      case 207011:
        return "依赖服务异常，$errorMsg";
      case 207012:
        return "远程调用异常（远端调用），$errorMsg";
      case 207013:
        return "设备入网失败";
      case 207014:
        return "Zigbee 子设备mac缺失";
      case 207015:
        return "Zigbee 子设备网关信息异常";
      case 207016:
        return "设备入云失败，原因：数据包格式错误";
      case 207017:
        return "设备入云失败，原因：找不到路由器";
      case 207018:
        return "设备入云失败，原因：密码错误";
      case 207019:
        return "设备入云失败，原因：连不上路由器";
      case 207020:
        return "设备入云失败，原因：DHCP 失败";
      case 207021:
        return "设备入云失败，原因：连云失败";
      case 207022:
        return "设备入云失败，原因：获取 URL 失败";
      case 207023:
        return "设备入云失败，原因：Wi-Fi 激活失败";
      case 207024:
        return "普通网关不支持 Zigbee 蓝牙能力";
      case 207025:
        return "网关未知错误";
      case 207026:
        return "普通网关子设备数量超限";
      case 207206:
        return "设备状态不可识别";
      case 207207:
        return "设备不支持获取 Wi-Fi 列表功能";
      case 207222:
        return "获取设备 Wi-Fi 列表信息失败";
      case 207226:
        return "获取设备 Wi-Fi 列表为空";
      case 207227:
        return "获取设备 Wi-Fi 指令下发超时";
      case 207209:
        return "配网信息错误";
      case 207210:
        return "找不到路由器";
      case 207211:
        return "路由器密码错误";
      case 207212:
        return "连接路由器失败";
      case 207213:
        return "DHCP 失败";
      case 207214:
        return "设备连云信息激活错误";
      case 207215:
        return "设备连云失败";
      case 207216:
        return "设备连云激活接口失败";
      case 207218:
        return "云端接口请求失败";
      case 207219:
        return "云端 iot-dns 连接失败";
      case 207220:
        return "设备配网超时";
      case 207221:
        return "连接设备蓝牙失败";
      case 207302:
        return "设备已被绑定";
      case 1007:
        return "设备已被其他用户绑定，不能重复绑定";
      case 411:
      case 1501:
        return "未能完成操作";
      case 135:
      case 148:
        return "配网失败";
      default:
        return errorMsg;
    }
  }

  bool get isWifiConfigError {
    switch (errorCode) {
      case 2:
      case 3:
      case 4:
      case 5:
      case 207009:
      case 207017:
      case 207018:
      case 207019:
      case 207020:
      case 207023:
      case 207210:
      case 207211:
      case 207212:
      case 207213:
        return true;
      default:
        return false;
    }
  }
}
